package com.example.demo.controller;

import com.example.demo.entity.ResultEntity;
import com.example.demo.service.serviceimpl.CommentServiceImpl;
import com.example.demo.vo.CommentVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/comment/")
@CrossOrigin
@RestController
public class CommentController {
    @Resource
    CommentServiceImpl commentService;

    // 根据商品ID获取评论列表
    @GetMapping("getCommentsByProductId")
    private ResultEntity getCommentsByProductId(@RequestParam("products_id") Integer products_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(commentService.getCommentsByProductId(products_id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取评论失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据用户ID获取评论列表
    @GetMapping("getCommentsByUserId")
    private ResultEntity getCommentsByUserId(@RequestParam("user_id") Integer user_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(commentService.getCommentsByUserId(user_id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取用户评论失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据ID获取评论详情
    @GetMapping("getCommentById")
    private ResultEntity getCommentById(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(commentService.getCommentById(id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取评论详情失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 添加评论
    @PostMapping("addComment")
    private ResultEntity addComment(@RequestBody CommentVo commentVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (commentService.addComment(commentVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("添加失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 更新评论
    @PutMapping("updateComment")
    private ResultEntity updateComment(@RequestBody CommentVo commentVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (commentService.updateComment(commentVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("修改失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 删除评论
    @DeleteMapping("deleteComment")
    private ResultEntity deleteComment(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (commentService.deleteComment(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除评论成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除评论失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    @GetMapping("getAverageRating")
    private ResultEntity getAverageRating(@RequestParam("products_id") Integer products_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            double avgRating = commentService.getAverageRatingByProductId(products_id);
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(avgRating);
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取平均评分失败");
            e.printStackTrace();
        }
        return resultEntity;
    }
}